package com.sishu.njrtsdms.service.impl;


import com.sishu.njrtsdms.domain.core.GenericServiceImpl;
import com.sishu.njrtsdms.domain.vo.SysFileVO;
import com.sishu.njrtsdms.jooq.codegen.tables.daos.SysFileDAO;
import com.sishu.njrtsdms.jooq.codegen.tables.daos.TeachConPrjProofFileDAO;
import com.sishu.njrtsdms.jooq.codegen.tables.pojos.SysFile;
import com.sishu.njrtsdms.service.SysFileService;
import com.sishu.njrtsdms.utils.file.FileUtils;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class SysFileServiceImpl extends GenericServiceImpl<SysFile, String>
        implements SysFileService {


    private final SysFileDAO sysFileDAO;

    private final TeachConPrjProofFileDAO teachConPrjProofFileDAO;

    public SysFileServiceImpl(SysFileDAO sysFileDAO,
                              TeachConPrjProofFileDAO teachConPrjProofFileDAO) {
        super(sysFileDAO);
        this.sysFileDAO = sysFileDAO;
        this.teachConPrjProofFileDAO = teachConPrjProofFileDAO;
    }


    @Override
    public SysFileVO fetchOneVOByUUID(String uuid) {
        return sysFileDAO.fetchOneVOByUUID(uuid);
    }

    @Override
    public void deleteFilesByUUIDs(List<String> uuidList) {
        // 删除文件信息
        // 1.先查看是否有佐证文件记录信息
        long count = teachConPrjProofFileDAO.countByUUIDs(uuidList);
        if (count > 0) {
            var tcpProofFileIdList = teachConPrjProofFileDAO.listTcpProofFileIdsByUUIDs(uuidList);
            teachConPrjProofFileDAO.deleteById(tcpProofFileIdList);
        }
        List<SysFile> sysFileList = sysFileDAO.fetchById(uuidList);
        // 2.删除对应文件信息
        sysFileDAO.deleteById(uuidList);
        // 3.删除磁盘上的文件
        sysFileList.forEach(sysFile -> FileUtils.deleteFile(sysFile.getDrivePath()));
    }
}